package com.songlanyun.modules.sensitive.controller.admin;

import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.sensitive.service.SystemSensitiveWordService;

import java.util.Arrays;
import java.util.Map;


/**
 * 平台，附加敏感词表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-11 11:06:47
 */
@RestController
@RequestMapping("admin/config/sensitiveword")
public class SystemSensitiveWordAdminController extends AbstractController{
    @Autowired
    private SystemSensitiveWordService systemSensitiveWordService;


    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = systemSensitiveWordService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 过滤传过来的词
     */
    @GetMapping("/filter")
    public R filter(@RequestParam String text) {
        String filter = systemSensitiveWordService.filter(text);
        return R.ok().put("data", filter);
    }


    /**
     * 添加附加敏感词
     */
    @SysLog("添加附加敏感词")
    @PostMapping("/putAll")
    public R putAll(@RequestBody String[] arr) {
        verifyAdmin();
        systemSensitiveWordService.put(arr);

        return R.ok();
    }

    /**
     * 删除附加敏感词
     */
    @SysLog("删除附加敏感词")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        verifyAdmin();
        systemSensitiveWordService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 添加和删除操作后都需要刷新操作
     * 刷新附加敏感词库
     */
    @SysLog("刷新附加敏感词库")
    @PostMapping("/flush")
    public R flush() {

        //将数据库中的数据库刷新到filter中
        systemSensitiveWordService.flush();
        return R.ok();
    }

}
